﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEditor.ProjectWindowCallback;
using UnityEngine;


namespace XFFSM
{
    public class FSMStateCreator : EndNameEditAction
    {

        string regex = "^[a-zA-Z][a-zA-Z0-9_]*$";

        public override void Action(int instanceId, string pathName, string resourceFile)
        {
            string fileName = Path.GetFileNameWithoutExtension(pathName);

            if (!Regex.Match(fileName, regex).Success) {

                Debug.LogErrorFormat("文件名不可用:{0}",fileName);
                return;
            }
            
            string content = AssetDatabase.LoadAssetAtPath<TextAsset>(resourceFile).text;
            content = content.Replace("{0}", fileName);

            FileStream stream = File.Create(pathName);
            stream.Write( Encoding.UTF8.GetBytes( content),0,content.Length);
            stream.Close();
            //stream.Write()
            AssetDatabase.Refresh();
            AssetDatabase.SaveAssets();
            //throw new System.NotImplementedException();
        }
    }

}

